/*
 * 文件名：BaseAction.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：qianfm
 * 修改时间：Sep 25, 2012
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.manager.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.wootide.cms.common.manager.AjaxUtil;
import com.wootide.cms.common.manager.BaseResult;
import com.wootide.cms.common.manager.Const;
import com.wootide.cms.common.manager.GeneralUtils;
import com.wootide.cms.common.manager.PageEntity;
import com.wootide.cms.common.manager.ResultMgr;


/**
 * 〈action 基类〉
 * 〈功能详细描述〉
 * @author qianfm
 * @version Sep 25, 2012
 * @see BaseAction
 * @since
 */

public class BaseAction
{
    
    
    public static final String ERROR = "error";
    
    public static final String FAILURE = "failure";
    
    public static final String INDEX = "index";
    
    public static final String LIST = "list";
    
    public static final String TO_GRANT = "toGrant";
    
    public static final String GRANT = "grant";
    
    public static final String SUCCESS = "success";
    
    public static final String DETAIL = "detail";
    
    public static final String ADD = "add";
    
    public static final String TO_ADD = "toAdd";
    
    public static final String TO_MOD = "toMod";
    
    public static final String MOD = "mod";
    
    public static final String MOD_SUCCESS = "modSuccess";
    
    public static final String ADD_SUCCESS = "addSuccess";
    
    
    public static String getExt(String fileName)
    {
        if (fileName == null)
        {
            return "";
        }
        int point = fileName.lastIndexOf('.');
        if (point > 0)
        {
            return fileName.substring(fileName.lastIndexOf("."));
        }
        else
        {
            return fileName;
        }
    }
    
    protected final String FAIL = "fail";
    
    protected final String HOME = "home";
    
    protected final String LOGIN = "login";
    
    protected final String LOGIN_RE = "relogin";
    
    protected final String LOGIN_REFERER = "refererlogin";
    
    private String msg;
    
    protected PageEntity page = null;
    
    protected String pageIndex = "1";
    
    protected final String REGISTER = "register";
    
    protected final String REGISTER_2 = "register_2";
    
    protected final String REGISTER_LINK = "register_link";
    
    /**
     * req 请求
     */
    private HttpServletRequest request;
    
    /**
     * res 响应
     */
    private HttpServletResponse response;
    
    private Map<String, Object> session;
    
    protected final String SUCCESS_GUEST = "success_guest";
    
    public void doResetAction()
    {
        
    }
    
    public String getLastUrlStr()
    {
        String str = ServletActionContext.getRequest().getServletPath();
        if (str.indexOf('/') == 0)
        {
            str = str.substring(1, str.length());
        }
        return str;
    }
    
    public String getMsg()
    {
        return this.msg;
    }
    
    public PageEntity getPage()
    {
        return page;
    }
    
    public String getPageIndex()
    {
        return pageIndex;
    }
    
    public String getRefererUrl()
    {
        return ServletActionContext.getRequest().getHeader("Referer");
    }
    
    /**
     * 获取request
     * Description: <br>
     * Implement: <br>
     * @return
     * @see
     * @return HttpServletRequest
     * @author qianfm
     */
    public HttpServletRequest getRequest()
    {
        if (GeneralUtils.isNull(request))
        {
            request = ServletActionContext.getRequest();
        }
        return this.request;
    }
    
    /**
     * 获取response
     * Description: <br>
     * Implement: <br>
     * @return
     * @see
     * @return HttpServletResponse
     * @author qianfm
     */
    public HttpServletResponse getResponse()
    {
        if (GeneralUtils.isNull(response))
        {
            response = ServletActionContext.getResponse();
        }
        return this.response;
    }
    
    public Map<String, Object> getSession()
    {
        return this.session;
    }
    
    public void responseResult(BaseResult br)
    {
        
        ResultMgr rtMgr = new ResultMgr(br, Const.JSON);
        String re = rtMgr.convertResult();
        
        //System.out.println(re);
        
        HttpServletResponse response = ServletActionContext.getResponse();
        AjaxUtil.sendResponseJsonText(response, re);
    }
    
    public void setMsg(String msg)
    {
        this.msg = msg;
    }
    
    public void setPage(PageEntity page)
    {
        this.page = page;
    }
    
    public void setPageIndex(String pageIndex)
    {
        this.pageIndex = pageIndex;
    }
    
    public void setSession(Map<String, Object> session_temp)
    {
        this.session = session_temp;
    }
    
    public String toActHint()
    {
        return SUCCESS;
    }
    
    public String toSuccess()
    {
        return SUCCESS;
    }
    
}
